import {
    createEnterprise,
    listUserEnterprise,
    getCurrentEnterprise,
    getEnterpriseById,
    getEnterpriseDepartment,
    listEnterpriseRole,
    listEnterpriseUser,
    setUserCurrentEnterprise,
    listProjectRole,
    listEnterpriseRoleTemplate,
    listProjectRoleTemplate,
    listUserBriefByEnterpriseId
} from '@/services/enterprise';
import { history } from 'umi';
import { message } from 'antd';
import { EXCELLENTPM_API } from '@/utils/request'
import Base64 from 'crypto-js/enc-base64'
import Utf8 from 'crypto-js/enc-utf8'

const EnterpriseModel = {
    namespace: 'enterprise',
    state: {
        currentEnterprise: {},
        enterpriseDepartment: {},
        enterpriseInfo: {},
        enterpriseRole: {},
        enterpriseUserBriefList: []
    },
    effects: {

        *createEntereprise({ payload }, { call, put }) {
            const response = yield call(createEnterprise, payload);
            if (response && response.success) {
                message.success("创建企业成功！")
                return response.success;
            }
        },
        *getEnterpriseById({ payload }, { call, put }) {
            const response = yield call(getEnterpriseById, payload);
            if (response && response.success) {
                yield put({
                    type: 'setEnterprise',
                    payload: response.data,
                });
            }
        },
        *getEnterpriseDepartment({ payload }, { call, put }) {
            const response = yield call(getEnterpriseDepartment, payload);
            if (response && response.success) {
                yield put({
                    type: 'setEnterpriseDepartment',
                    payload: response.data,
                });
            }
        },
        *listEnterpriseUser({ payload }, { call, put }) {
            const response = yield call(listEnterpriseUser, payload && payload.params);
            yield put({
                type: 'setEnterpriseUser',
                payload: response.data,
            });
        },
        *listUserBriefByEnterpriseId({ payload }, { call, put }) {
            const response = yield call(listUserBriefByEnterpriseId, payload);
            yield put({
                type: 'setEnterpriseUserBrief',
                payload: response.data,
            });
        },
        *listEnterpriseRoleTemplate({ payload }, { call, put }) {
            const response = yield call(listEnterpriseRoleTemplate, payload);
            if (response && response.success) {
                yield put({
                    type: 'setEnterpriseRoleTemplateList',
                    payload: response.data,
                });
            }
        },

        *listProjectRoleTemplate({ payload }, { call, put }) {
            const response = yield call(listProjectRoleTemplate, payload);
            if (response && response.success) {
                yield put({
                    type: 'setProjectRoleTemplateList',
                    payload: response.data,
                });
            }
        },


        *listEnterpriseRole({ _ }, { call, put }) {
            const response = yield call(listEnterpriseRole);
            if (response && response.success) {
                yield put({
                    type: 'setEnterpriseRoleList',
                    payload: response.data,
                });
            }
        },

        *listProjectRole({ payload }, { call, put }) {
            const response = yield call(listProjectRole);
            if (response && response.success) {
                yield put({
                    type: 'setProjectRoleList',
                    payload: response.data,
                });
            }
        },

        *setCurrentEnterprse({ payload }, { call, put }) {
            const response = yield call(setUserCurrentEnterprise, {
                enterpriseId: payload.enterpriseId
            });
            return response.success;
        },
        *getUserCurrentEnterprise(_, { call, put }) {
            const response = yield call(getCurrentEnterprise);
            if (response && response.success) {
                if (response.data) {
                    localStorage.setItem('enterpriseId', response.data.enterpriseId)
                    console.log("cookie", document.cookie)
                    if (!document.cookie) {
                        localStorage.setItem('e_id', Base64.stringify(Utf8.parse(response.data.enterpriseId)))
                    }
                }
                yield put({
                    type: 'setUserCurrentEnterprise',
                    payload: response.data ? response.data : { enterpriseId: 0 },
                });
            }
        },
        *listUserEnterprise(_, { call, put }) {
            const response = yield call(listUserEnterprise);
            if (response) {
                yield put({
                    type: 'setUserEnterprseList',
                    payload: response.data,
                });
            }
        },

    },
    reducers: {
        setUserCurrentEnterprise(state, action) {
            return { ...state, currentEnterprise: action.payload }
        },
        setUserEnterprseList(state, action) {
            return { ...state, userEnterpriseList: action.payload }
        },
        setEnterpriseDepartment(state, action) {
            return { ...state, enterpriseDepartment: action.payload }
        },
        setEnterprise(state, action) {
            return { ...state, enterpriseInfo: action.payload }
        },
        setEnterpriseUser(state, action) {
            return { ...state, enterpriseUser: action.payload }
        },
        setEnterpriseUserBrief(state, action) {
            return { ...state, enterpriseUserBriefList: action.payload }
        },
        setEnterpriseRoleTemplateList(state, action) {
            return { ...state, enterpriseRoleTemplateList: action.payload }
        },
        setProjectRoleTemplateList(state, action) {
            return { ...state, projectRoleTemplateList: action.payload }
        },
        setEnterpriseRoleList(state, action) {
            return { ...state, enterpriseRoleList: action.payload }
        },
        setProjectRoleList(state, action) {
            return { ...state, projectRoleList: action.payload }
        }
    },
};
export default EnterpriseModel;
